package com.lance.core.verticle;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hazelcast.util.function.Consumer;
import com.lance.core.base.BC;
import com.lance.core.base.ClassObject;
import com.lance.core.base.VResfulMapping;
import com.lance.core.util.ClassUtil;

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;

public class ClusterStarter {	
	private static final Logger log = LogManager
			.getLogger(ClusterStarter.class);

	public static void main(String[] args) throws Exception {
		BC.SERVICECLASSS =  ClassUtil.anSimpleNames("com.lance",VResfulMapping.class);
		BC.ct = new ClassPathXmlApplicationContext("bean.xml");
		final VertxOptions options = new VertxOptions();
		options.setClustered(true);
		final DeploymentOptions opts =new DeploymentOptions();
		Consumer<Vertx> runner = vertx -> {
			vertx.deployVerticle(RootVerticle.class.getName(),opts);
			//注册resful服务
			for (ClassObject classObject : BC.SERVICECLASSS.values()) {
				vertx.deployVerticle(classObject.getClassName(),opts);
			}
			vertx.deployVerticle(SocketVerticle.class.getName(),opts);
		};
		if (options.isClustered()) {
			Vertx.clusteredVertx(
					options,
					result -> {
						if (result.succeeded()) {
							Vertx vertx = result.result();
							runner.accept(vertx);
							log.info("pushservice is running with cluster by hazelcast.");
						} else {
							log.error("cluster running with error: "
									+ result.cause().getMessage());
						}
					});            
		} else {
			Vertx vertx = Vertx.vertx(options);
			runner.accept(vertx);
		}
	}
}
